package com.sekill.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seckill.common.core.constant.Constants;
import com.seckill.common.core.domain.R;
import com.seckill.common.core.exception.BaseException;
import com.seckill.common.core.utils.SecurityUtils;
import com.seckill.common.core.utils.StringUtils;
import com.seckill.common.core.utils.bean.BeanUtils;
import com.seckill.common.redis.service.RedisService;
import com.seckill.common.security.service.TokenService;
import com.seckill.system.api.RemoteLogService;
import com.seckill.system.api.RemoteMsgService;
import com.seckill.system.api.model.LoginUser;
import com.sekill.user.entity.TbUser;
import com.sekill.user.service.ITbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author gao
 * @since 2021-06-18
 */
@RestController
@RequestMapping("/tb-user")
public class TbUserController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private ITbUserService iTbUserService;

    @Autowired
    private RemoteLogService remoteLogService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private RemoteMsgService remoteMsgService;

    @GetMapping("login")
    public R<?> login(@RequestParam("phone") String phone,@RequestParam("phone") String password) {
        if (StringUtils.isAnyBlank(phone, password))
        {
            remoteLogService.saveLogininfor(phone, Constants.LOGIN_FAIL, "用户/密码必须填写");
            throw new BaseException("手机号/验证码必须填写");
        }
        /*Boolean aBoolean = remoteMsgService.checkVerifyCode(phone, password);
        if (!aBoolean) {
            return R.fail("验证码不正确");
        }*/
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        TbUser one = iTbUserService.getOne(queryWrapper);
        if (one == null) {
            throw new BaseException("用户不存在");
        }
        com.seckill.system.api.domain.TbUser tbUser = new com.seckill.system.api.domain.TbUser();
        BeanUtils.copyProperties(one, tbUser);
        tokenService.createToken2(tbUser);
        return R.ok(tokenService.createToken2(tbUser));
    }

    @GetMapping("getUserId")
    public Long getUserId(String token) {
        System.out.println(token+"1111");
        Object cacheObject = redisService.getCacheObject("login_tokens:" + token);
        //if (cacheObject != null) {
            LoginUser aa = (LoginUser) cacheObject;
            System.out.println(aa.getUsername()+"  --  "+aa.getUserid()+"11112222");
       // }
        System.out.println(cacheObject+"11113333");
        return aa.getUserid();
    }


}
